﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using OPM.Models;

namespace OPM
{
    public static class UserManager
    {
        private static List<OpmUser> _userCaches = null;
        public static List<OpmUser> UsersCache
        {
            get
            {
                if (_userCaches == null)
                {
                    ReloadUserCache();
                }
                return _userCaches;
            } 
        }

        private static void ReloadUserCache()
        {
            if (_userCaches == null || !_userCaches.Any())
            { 
                _userCaches = new OpmContext().OpmUsers.ToList();
            }
        }
        private static List<Menu> _menusCache = null;
        public static List<Menu> MenusCache
        {
            get
            {
                if (_menusCache == null)
                {
                    ReloadMenuCache();
                }
                return _menusCache;
            }
        }

        private static void ReloadMenuCache()
        {
            if (_menusCache == null || !_menusCache.Any())
            {
                _menusCache = new OpmContext().Menus.ToList();
            }
        }

        public static void Create(OpmUser user)
        {
            var db = new OpmContext();
            db.OpmUsers.Add(user);
            db.SaveChanges();
        }


        public static OpmUser CurrentUser
        {
            get
            {
                return HttpContext.Current.Session["LoginOpmUser"] as OpmUser;
            }
        }
        
        public static OpmUser Find(string userName, string password)
        {
            if (userName == null) return null;

            userName = userName.Trim().ToLower();

            return UsersCache.FirstOrDefault(o => o.UserName.ToLower() == userName && o.Password == password);
        }

        public static bool Login(string userName, string password)
        { 
            OpmUser user = Find(userName, password);
            HttpContext.Current.Session["LoginOpmUser"] = user;

            return (user != null);
        }

        public static bool IsLogin
        {
            get { return CurrentUser != null; }
        }

        public static List<Menu> GetMenu()
        {
            List<Menu> menues = new List<Menu>();
            if (UserManager.CurrentUser.IsAdmin) menues = MenusCache.ToList();
            
            foreach (var roleMenus in CurrentUser.UserRoles.Select(o => o.OpmRole.RoleMenus))
            {
                foreach (var roleMenu in roleMenus)
                {
                    if (!menues.Exists(o => o.MenuId == roleMenu.MenuId))
                    {
                        menues.Add(roleMenu.Menu);
                    }
                }
            }
            menues = menues.OrderBy(o => o.Code).ToList();
            return menues;
        }
    }
}